#include "modelcollections.h"

ModelCollections::ModelCollections(QObject *parent) :
    QAbstractListModel(parent)
{
    collList = ListMaintence::getInstance()->collectionsList();
}
QVariant ModelCollections::data(const QModelIndex &index, int role) const
{  
    if (index.row() < 0 || index.row() > collList.length())
        return QVariant();

    if(role == CollName)
    {
        return collList.value(index.row());
    }
}

int ModelCollections::rowCount(const QModelIndex &parent) const
{
    Q_UNUSED(parent)
    return collList.length();
}

QHash<int, QByteArray> ModelCollections::roleNames() const
{
    QHash<int, QByteArray> roles;
    roles[CollName] = "name";
    return roles;
}

void ModelCollections::addColl(const QString &_s)
{
    ListMaintence::getInstance()->addCollection(_s);
    collList.clear();
    collList = collList = ListMaintence::getInstance()->collectionsList();
    beginInsertRows(QModelIndex(),collList.indexOf(_s),collList.indexOf(_s));
    endInsertRows();
}

void ModelCollections::remColl(const QString &_s)
{
    beginRemoveRows(QModelIndex(),collList.indexOf(_s),collList.indexOf(_s));
    ListMaintence::getInstance()->remCollection(_s);
    collList.clear();
    collList = collList = ListMaintence::getInstance()->collectionsList();
    endRemoveRows();
}

void ModelCollections::chosen(const QString &_s)
{
    ListMaintence::getInstance()->setCollectionName(_s);   
}
